#include "utilities/GlobalState.h"
#include "utilities/GlobalStatistic.h"

shared_ptr<GlobalState> GlobalState::m_Instance;
UsrModeLock GlobalState::m_LockForCreation;

shared_ptr<GlobalState> GlobalState::getInstance()
{
	if (!m_Instance) {
		m_LockForCreation.lock();
		
		if (!m_Instance)
			m_Instance.reset(new GlobalState());

		m_LockForCreation.unLock();
		m_LockForCreation.destroy();
	}

	return m_Instance;
}


GlobalState::GlobalState()
{
	m_Statistic.reset(new GlobalStatistic());
}

void GlobalState::setState(const QString& key,const QVariant& value,bool sendSignal)
{
	UsrModeLockLocker locker(&m_Lock);

	m_State.insert(key,value);
	if(sendSignal) 
		emit globalStateChanged();
}

QVariant GlobalState::getState(const QString& key)
{
	UsrModeLockLocker locker(&m_Lock);
	return m_State.value(key);
}
